﻿using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using HashFoo.MigratorFoo;
using HashFoo.Nhib.Spatial;
using MigratorFoo.MockAssembly.Data.Models;
using NHibernate.Cfg;
using NHibernate.Dialect;

namespace MigratorFoo.MockAssembly.Data.Persistence
{
    [DbConfigurationFixture]
    public static class MockConfigurator
    {
        [DbConfiguration("default", true)]
        public static Configuration DefaultConfiguration
        {
            get
            {
                var config = BuildMSSqlConfiguration("Data Source=(local);Initial Catalog=MigratorFoo.Test;Integrated Security=True");

                // need to come up with a good validation solution...
                //var engine = new ValidatorEngine();
                //var validationConfig = new NhValidationConfiguration();
                //engine.Configure(validationConfig);

                //config.Initialize(engine);

                return config;
            }
        }

        [DbConfiguration("test")]
        public static Configuration TestConfiguration
        {
            get { return BuildMSSqlConfiguration("Data Source=(local);Initial Catalog=MigratorFoo.Test;Integrated Security=True"); }
        }

        public static Configuration BuildConfiguration(IPersistenceConfigurer databaseConfig)
        {
            return Fluently.Configure()
                .Database(databaseConfig)
                .Mappings(
                    m => m.AutoMappings.Add(
                        AutoMap
                            .AssemblyOf<Contact>()
                            .Where(t => t.Namespace == typeof(Contact).Namespace)
                            .Conventions.AddFromAssemblyOf<HashFooGeographyTypeConvention>()))
                .BuildConfiguration();
        }

        public static Configuration BuildMSSqlConfiguration(string connectionString)
        {
            return BuildConfiguration(MsSqlConfiguration.MsSql2008
                                          .ConnectionString(
                                              c => c.Is(connectionString))
                                          .Dialect<MsSql2008Dialect>());
        }
    }
}